package Array_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @description:
 * @Author yyp
 * @time
 */
public class ArrayExercise {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("sanguo",50);
        books[1] = new Book("shuihuzhuan",200);
        books[2] = new Book("hongloumeng",150);
        books[3] = new Book("xiyouji",20);
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if(o1.getPrice()>o2.getPrice()){
                    return 1;
                }else if(o1.getPrice()<o2.getPrice()){
                    return -1;
                }else {
                    return 0;
                }
            }
        });
        System.out.println(Arrays.toString(books));
        //如果传入的是类，那么toString方法调用的时类中定义的toString方法
    }
}

class Book{
    String name;
    int price;
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
    public Book(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}
